package com.hyd.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.base.dao.model.BFile;
import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import com.base.service.BFileService;
import com.base.util.BeanUtils;
import com.base.util.QrUtil;
import com.base.util.StringUtil;
import com.hyd.daoEx.model.ChemicalTypeEx;
import com.item.ConstantsCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;
import com.hyd.dao.model.ChemicalType;
import com.hyd.dao.model.ChemicalTypeExample;
import com.hyd.service.ChemicalTypeService;

/**
@author sun
*/
@RequestMapping("chemicalType")
@Controller
@LoginFilter
public class ChemicalTypeController extends CoreController{

    @Autowired
    private ChemicalTypeService chemicalTypeService;
	@Autowired
	private BFileService fileService;

    @RequestMapping("/list")
	@ResponseBody
    public String list(Integer page, Integer rows, String name) throws Exception {
    	PaginationSupport.byPage(page, rows);
    	ChemicalTypeExample example = new ChemicalTypeExample();
		if(StringUtil.isNotBlank(name)){
			example.or().andNameLike("%"+name+"%");
			example.or().andChemicalNameLike("%"+name+"%");
		}
    	List<ChemicalType> list = chemicalTypeService.selectByExampleWithBLOBs(example);
//		List<ChemicalType> ret = new ArrayList<>();
//		for(ChemicalType ex : list){
//			ChemicalType c = new ChemicalType();
//			BeanUtils.copyProperties(c,ex);
//			if(StringUtil.isNotBlank(ex.getQrImg())){
//				QrUtil.encoderQRCode(ex.getId()+"", ConstantsCode.getProperty("file.path"),"png");
//				QrUtil.decoderQRCode( ConstantsCode.getProperty("file.path"));
//				c.setQrImg(fileService.selectByPrimaryKey(ex.getId()));
//			}
//
//
//		}

      	return page(list);
    }

    @RequestMapping("/save")
	@ResponseBody
    public String save(ChemicalType chemicalType){
    	if (chemicalType.getId() == null){

			chemicalTypeService.insert(chemicalType);
			UUID uuid = UUID.randomUUID();
			String path = ConstantsCode.getProperty("file.path")+"qr/"+uuid+".png";
			QrUtil.encoderQRCode(chemicalType.getId()+"",path,"png",500);
//			fileService.uploadFile(file.get(), file.getName(), BFileService.LOCAL);
//			BFileService.
			BFile file = new BFile();
			file.setFileId(uuid+"");
			file.setFileBelong("local");
			file.setFilePath("qr/"+uuid+".png");
			file.setFileMinitype("image/jpeg");
			file.setFileName("tem_"+uuid.toString());
			file.setFileState(1);
			file.setFileCreatetime(new Date());
			fileService.insert(file);
			chemicalType.setQrImg(uuid.toString());
			chemicalTypeService.updateByPrimaryKeySelective(chemicalType);
    	}else{
    		chemicalTypeService.updateByPrimaryKeySelective(chemicalType);
    	}
       	return ok();
    }

    @RequestMapping("/findById")
	@ResponseBody
    public String find(Integer id){
    	ChemicalType chemicalType = chemicalTypeService.selectByPrimaryKey(id);
       	return ok(chemicalType);
    }

    @RequestMapping("/del")
	@ResponseBody
    public String del(String id){
    	String[] ids = id.split(",");
    	for (String str : ids){
    		chemicalTypeService.deleteByPrimaryKey(Integer.parseInt(str));
    	}
       	return ok();
    }

	@RequestMapping("/isExist")
	@ResponseBody
	public String isExist(ChemicalType chemicalType){
		ChemicalTypeExample example = new ChemicalTypeExample();
		//example.createCriteria().andNameEqualTo(chemicalType.getName());
		List<ChemicalType> list = chemicalTypeService.selectByExample(example);
		String strs = "";
		int count = 0;
		for(ChemicalType ex : list){
			if(ex.getName().equals(chemicalType.getName())){
				count++;
			}
			if(StringUtil.isNotBlank(ex.getDicInfo())){
				if(ex.getDicInfo().contains(chemicalType.getName())){
					count++;
				}
			}

		}
//		int count = chemicalTypeService.countByExample(example);
		if(count >= 1){
			return msg(-1,chemicalType.getName()+"已存在,请检查");
		}
		return ok();
	}

}